//
//  TuyaLoginStep.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/4/16.
//
//- (void)loginByPhone:(NSString *)countryCode
//         phoneNumber:(NSString *)phoneNumber
//            password:(NSString *)password
//             success:(nullable ThingSuccessHandler)success
//             failure:(nullable ThingFailureError)failure;
import Combine
import ThingSmartBaseKit

struct TuyaLoginPwdStep: LoginStep {
    struct InputData {
        let contryCode = "86"
        let account: String
        let password: String
    }
    
    func execute(_ input: InputData) -> AnyPublisher<Void, Error> {
        
        if input.account.isEmail {
            return Future<Void, Error> { promise in
                ThingSmartUser.sharedInstance().login(byEmail: input.contryCode, email: input.account, password: input.password, success:{
                    promise(.success(()))
                }, failure: { error in
                    if let error = error {
                        promise(.failure(error))
                    } else {
                        promise(.failure(NSError(domain: "涂鸦错误", code: -1, userInfo: [NSLocalizedDescriptionKey: "未知错误"])))
                    }
                })
            }.eraseToAnyPublisher()
        } else {
            return Future<Void, Error> { promise in
                ThingSmartUser.sharedInstance().login(byPhone: input.contryCode, phoneNumber: input.account, password: input.password, success: {
                    promise(.success(()))
                }, failure: { error in
                    if let error = error {
                        promise(.failure(error))
                    } else {
                        promise(.failure(NSError(domain: "涂鸦错误", code: -1, userInfo: [NSLocalizedDescriptionKey: "未知错误"])))
                    }
                })
            }.eraseToAnyPublisher()
        }
    }
}

